﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StandXunYouServer.IServices;
using StandXunYouServer.Utility;

namespace StandXunYouServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        //登录API
        private readonly ILogger _logger;
        private readonly ILoginServices _loginConn;

        public LoginController(ILogger<LoginController> logger, ILoginServices logginConn)
        {
            _logger = logger;
            _loginConn = logginConn;
        }

        /// <summary>
        /// 获取用户数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SigninAsync([FromForm]string userId)
        {
            try
            {
                var model = await _loginConn.GetUserQueryAsync(userId);
                if(model != null)
                    return Ok(BaseState.CreateInstance(200, "登录成功", model, 1));
                else
                    return Ok(BaseState.CreateInstance(-1, "账号或密码错误"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, "获取用户数据失败");
            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求服务器失败，请稍后重试"));
        }
    }
}
